using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class ChucNangDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveChucNang(ChucNang chucnang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddChucNang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddChucNang";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, chucnang.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, chucnang.MaSo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, chucnang.Ten);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Nhom", DbType.String, ParameterDirection.Input, chucnang.Nhom);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			return result;
		}
		
		// Get all ChucNang in DB
		public ResultObj GetAllChucNang( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<ChucNang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllChucNang", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<ChucNang>();
			ChucNang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new ChucNang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
            item.Nhom = Convert.ToString(reader["Nhom"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all ChucNang in DB with condition
		public ResultObj GetAllChucNangByID(ChucNang chucnang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<ChucNang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllChucNangByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, chucnang.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<ChucNang>();
			ChucNang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new ChucNang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
            item.Ten = Convert.ToString(reader["Ten"]);
            item.Nhom = Convert.ToString(reader["Nhom"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a ChucNang out of DB with condition
		public ResultObj DeleteChucNang(ChucNang chucnang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteChucNang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteChucNang";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, chucnang.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of PhanQuyen from DB with condition
		public ResultObj GetPhanQuyen(int IdPhanQuyen)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhanQuyen> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhanQuyenByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhanQuyen);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhanQuyen>();
			PhanQuyen item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhanQuyen();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdChucVu = Convert.ToInt32(reader["IdChucVu"]);
			item.IdChucNang = Convert.ToInt32(reader["IdChucNang"]);
			item.Quyen = Convert.ToInt32(reader["Quyen"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
